package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.dto.MenuPermissionDTO;
import com.example.service.MenuPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单权限绑定控制器
 */
@RestController
@RequestMapping("/api/menu-permission")
@CrossOrigin
public class MenuPermissionController {

    @Autowired
    private MenuPermissionService menuPermissionService;

    /**
     * 为菜单绑定权限
     */
    @PostMapping("/bind")
    @RequirePermission("menu:permission:bind")
    public Result<String> bindPermissionsToMenu(@RequestBody MenuPermissionDTO dto) {
        return menuPermissionService.bindPermissionsToMenu(dto);
    }

    /**
     * 获取菜单绑定的权限ID列表
     */
    @GetMapping("/menu/{menuId}")
    @RequirePermission("menu:permission:view")
    public Result<List<Long>> getMenuPermissions(@PathVariable Long menuId) {
        return menuPermissionService.getMenuPermissions(menuId);
    }

    /**
     * 获取权限绑定的菜单ID列表
     */
    @GetMapping("/permission/{permissionId}")
    @RequirePermission("menu:permission:view")
    public Result<List<Long>> getPermissionMenus(@PathVariable Long permissionId) {
        return menuPermissionService.getPermissionMenus(permissionId);
    }

    /**
     * 删除菜单的所有权限绑定
     */
    @DeleteMapping("/menu/{menuId}")
    @RequirePermission("menu:permission:delete")
    public Result<String> removeMenuPermissions(@PathVariable Long menuId) {
        return menuPermissionService.removeMenuPermissions(menuId);
    }

    /**
     * 删除权限的所有菜单绑定
     */
    @DeleteMapping("/permission/{permissionId}")
    @RequirePermission("menu:permission:delete")
    public Result<String> removePermissionMenus(@PathVariable Long permissionId) {
        return menuPermissionService.removePermissionMenus(permissionId);
    }

    /**
     * 检查菜单权限绑定是否存在
     */
    @GetMapping("/check")
    @RequirePermission("menu:permission:view")
    public Result<Boolean> checkMenuPermissionExists(@RequestParam Long menuId, @RequestParam Long permissionId) {
        return menuPermissionService.checkMenuPermissionExists(menuId, permissionId);
    }

    /**
     * 批量为多个菜单绑定权限
     */
    @PostMapping("/batch-bind")
    @RequirePermission("menu:permission:bind")
    public Result<String> batchBindPermissionsToMenus(@RequestParam List<Long> menuIds, @RequestParam List<Long> permissionIds) {
        return menuPermissionService.batchBindPermissionsToMenus(menuIds, permissionIds);
    }

    /**
     * 同步更新角色权限
     */
    @PostMapping("/sync-role-permissions/{menuId}")
    @RequirePermission("menu:permission:sync")
    public Result<String> syncRolePermissions(@PathVariable Long menuId) {
        return menuPermissionService.syncRolePermissions(menuId);
    }
} 